package com.chyer.logistics_interest.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PageMergeUtil {


    private PageInfo pageFromAService(JSONObject pageQuery){
        return new PageInfo();
    }

    private PageInfo pageFromBService(JSONObject pageQuery){
        return new PageInfo();
    }


    public PageInfo mergePageInfo(JSONObject pageQuery){

        int originaPageNum = pageQuery.getInteger("pageNum");
        int originalPageSize = pageQuery.getInteger("pageSize");;

        PageInfo pageInfo = new PageInfo();
        //先查询A服务的分页列表
        pageInfo = pageFromAService(pageQuery);
        if(pageInfo == null || pageInfo.getTotal() == 0){
            //如果A服务没有数据，则查询B服务
            return pageFromBService(pageQuery);
        }
        if(pageInfo != null && pageInfo.getRecords() != null && pageInfo.getRecords().size() > 0){
            int aTotal = pageInfo.getTotal();
            List aRecords = pageInfo.getRecords();
            int aCurrentPageSize = aRecords.size();
            if(aTotal > originalPageSize * originaPageNum && aCurrentPageSize == originalPageSize){
                //A服务有数据，且当前页的数据量满足当前页的查询条件，不需要再使用B服务数据进行补充
                PageInfo bPageInfo = pageFromBService(pageQuery);
                if(bPageInfo != null){
                    int bTotal = bPageInfo.getTotal();
                    pageInfo.setTotal(aTotal + bTotal);
                }

                return pageInfo;
            }else{
                //A服务有数据，但当前页的数据量不满足当前页的查询条件,需要使用B服务数据进行补充
                int diff = originalPageSize * originaPageNum - aTotal;
                //不超过整页
                if(diff <= originalPageSize){
                    pageQuery.put("pageNum", 1);
                    pageQuery.put("pageSize", diff);
                    PageInfo bPageInfo = pageFromBService(pageQuery);
                    if(bPageInfo != null && bPageInfo.getRecords() != null && bPageInfo.getRecords().size() > 0){
                        aRecords.addAll(bPageInfo.getRecords());
                        pageInfo.setTotal(aTotal + bPageInfo.getTotal());
                        pageInfo.setRecords(aRecords);
                    }
                    return pageInfo;
                }else{
                    //所查页数已超过A服务的总页数，需要完全从B服务查询
                    //需要考虑到：b服务的第一页数据已经为A服务补充过一部分数据的情况
                    //总的思路：B服务需要连着查询两页的数据，然后去掉b服务数据已经为A服务补充的数据,
                    //再从b剩下的数据中截取出满足索引的数据
                    List bList = new ArrayList();
                    int bTotal = 0;
                    int startPageNum = diff / originalPageSize;
                    int bStartIndex = originalPageSize * (startPageNum - 1) + 1;
                    log.info("==== 当前需要查询第{}页数据，每页{}条，需要从b服务查询第{}页数据补充",
                            originaPageNum, originalPageSize, startPageNum);
                    pageQuery.put("pageNum", startPageNum);
                    pageQuery.put("pageSize", originalPageSize);
                    PageInfo bPageInfo = pageFromBService(pageQuery);
                    if(bPageInfo != null && bPageInfo.getRecords() != null && bPageInfo.getRecords().size() > 0){
                        log.info("==== 查询b服务第{}页，每页{}条，得到记录数{}，b服务总记录数",
                                startPageNum, originalPageSize, bPageInfo.getRecords().size(), bPageInfo.getTotal());
                        bTotal = bPageInfo.getTotal();
                        bList.addAll(bPageInfo.getRecords());
                    }
                    int bMaxPage = bTotal / originalPageSize;
                    if(bTotal % originalPageSize != 0){ bMaxPage = bMaxPage + 1;}
                    startPageNum = startPageNum + 1;
                    if(startPageNum <= bMaxPage){
                        pageQuery.put("pageNum", startPageNum);
                        pageQuery.put("pageSize", originalPageSize);
                        PageInfo bPageInfo2 = pageFromBService(pageQuery);
                        if(bPageInfo2 != null){
                            bTotal = bPageInfo2.getTotal();
                            bList.addAll(bPageInfo2.getRecords());
                        }
                    }else{
                        log.info("==== b服务已无更多数据, 无需再继续查了  =====");
                    }

                    if(aTotal + bTotal > originalPageSize * (originaPageNum - 1)){
                        int a = originalPageSize * (originaPageNum - 1) + 1; //目标页码起始索引
                        int b = originalPageSize * originaPageNum; //目标页码结束索引
                        int c = bStartIndex + aTotal; //b服务起始索引(拼接数据起始位置)
                        int d = c + bList.size(); //拼接数据结束位置
                        int startIndex = Math.max(a, c); //重合区间取最小
                        int endIndex = Math.min(b, d) + 1; //重合区间取最大
                        if(startIndex <= endIndex){
                            log.info("==== 拼接数据区间为：{} - {} ====", startIndex, endIndex);
                            startIndex = startIndex - c; //开始坐标减去b服务起始索引
                            endIndex = endIndex - c; //结束坐标减去b服务起始索引
                            endIndex = Math.min(bList.size(), endIndex);//结束坐标避免超出b服务数据
                            startIndex = Math.max(0, startIndex); //开始坐标避免小于0
                            aRecords = bList.subList(startIndex, endIndex);
                        }else {
                            log.info("==== 无需拼接数据 ====");
                        }
                        pageInfo.setTotal(aTotal + bTotal);
                        pageInfo.setRecords(aRecords);
                        return pageInfo;
                    }
                }
            }
        }

        return pageInfo;
    }



}
